package com.hrbxlh.virtualization.controller.manage;

import com.hrbxlh.virtualization.param.ClusterCreateParam;
import com.hrbxlh.virtualization.param.ClusterUpdateParam;
import com.hrbxlh.virtualization.result.Result;
import com.hrbxlh.virtualization.service.ClusterService;
import com.hrbxlh.virtualization.vo.ClusterVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/manage/cluster")
public class ManageClusterController {

    @Autowired
    private ClusterService clusterService;

    /**
     * 创建集群
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result createCluster(@RequestBody @Valid ClusterCreateParam clusterCreateParam) {
        clusterService.createCluster(clusterCreateParam);
        return Result.success();
    }

    /**
     * 查询集群列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<List<ClusterVO>> listClusters() {
        List<ClusterVO> clusterList = clusterService.listClusters();
        return Result.success(clusterList);
    }

    /**
     * 查询集群详情
     *
     * @param id 集群Id
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Result<ClusterVO> queryCluster(Long id) {
        ClusterVO clusterVO = clusterService.queryCluster(id);
        return Result.success(clusterVO);
    }

    /**
     * 修改集群
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateCluster(@RequestBody @Valid ClusterUpdateParam clusterUpdateParam) {
        clusterService.updateCluster(clusterUpdateParam);
        return Result.success();
    }

    /**
     * 删除集群
     *
     * @param id       集群Id
     * @param isForced 是否强制删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result deleteCluster(Long id, @RequestParam(required = false, defaultValue = "false") Boolean isForced) {
        clusterService.deleteCluster(id, isForced);
        return Result.success();
    }
}
